'''
Created on 16 Apr 2012

@author: karulis
'''

from PySide import QtGui
from BaseTab import BaseTab

class DeleteTab(BaseTab):
    '''
    classdocs
    '''


    def __init__(self, parent = None):
        '''
        Constructor
        '''
        super(DeleteTab, self).__init__(parent)

        topLabel = QtGui.QLabel("Choose user:")

        self.usersListBox = QtGui.QListWidget()

        deleteButton = QtGui.QPushButton("Delete user")
        deleteButton.clicked.connect(self.delete_clicked)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(topLabel)
        layout.addWidget(self.usersListBox)
        layout.addWidget(deleteButton)
        self.setLayout(layout)

        self.refresh_gui()

    def refresh_gui(self):
        self.usersListBox.clear()
        users = self.get_users().keys()
        self.usersListBox.insertItems(0, users)

    def delete_clicked(self):
        row = self.usersListBox.currentRow()
        if row >= 0:
            user = self.usersListBox.item(row).text()
            ret = self.execute("userdel " + user)
            if "" != ret:
                self.show_message(ret)
            else:
                self.refresh.emit()
